package io.netty.protocodec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 自定义消息解码器
 *
 * @author Verlet
 * @date 2023-02-11 00:47
 */
@Slf4j
public class MsgDecode extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        log.info("==========调用解码器==========");
        int length = byteBuf.readInt();
        byte[] bytes = new byte[length];
        byteBuf.readBytes(bytes);
        MsgProto msgProto = new MsgProto();
        msgProto.setLength(length);
        msgProto.setData(bytes);
        list.add(msgProto);
    }
}
